//#include "StdAfx.h"
#include "..\inc\MD5Poco.h"
#include <fstream>
#include <poco/MD5Engine.h>
#include <DoubleBuffering.h>

CMD5::CMD5(void)
{
}

CMD5::~CMD5(void)
{
}

std::string CMD5::GetMD5(const std::string &aString)
{
	Poco::MD5Engine aMD5Engine;
	aMD5Engine.update(aString.c_str(), aString.length() );

	return Poco::DigestEngine::digestToHex(aMD5Engine.digest());
	//return "";
}

std::string CMD5::GetMD5(const char* aData, int aLength)
{
	Poco::MD5Engine aMD5Engine;
	aMD5Engine.update(aData, aLength);

	return Poco::DigestEngine::digestToHex(aMD5Engine.digest());
}
std::string CMD5::GetMD5ofFile(const std::string& aFileName)
{
	std::ifstream fin(aFileName.c_str(), std::ios::binary);
	if(! fin)
		return string("");
	char szLargeBuff[BUFF_LEN+1] = {0};
	char szBuff[DATA_LEN+1] = {0};
	CDoubleBuffering oDoubleBuffering(fin, szLargeBuff, BUFF_LEN, DATA_LEN);
	int iRead;
	Poco::MD5Engine aMD5Engine;
	while((iRead=oDoubleBuffering.GetData(szBuff)) > 0)
		aMD5Engine.update(szBuff, iRead);
	fin.close();
	std::string aResult = Poco::DigestEngine::digestToHex(aMD5Engine.digest()) ;
	return aResult;
}